using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    public class clsCHI_TIET_PDH_COMPONENT
    {
        public clsCHI_TIET_PDH_COMPONENT(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }

        private CHI_TIET_PDH_COMPONENT GetDataRow(DataRow row)
        {
            CHI_TIET_PDH_COMPONENT obj = new CHI_TIET_PDH_COMPONENT();
            try
            {
                if (row["MaComponent"] != DBNull.Value)
                    obj.MaComponent = long.Parse(row["MaComponent"].ToString());
            }
            catch { }
            try
            {
                if (row["MaChiTietPDH"] != DBNull.Value)
                    obj.MaChiTietPDH = int.Parse(row["MaChiTietPDH"].ToString());
            }
            catch { }
            try
            {
                if (row["MaThietBi"] != DBNull.Value)
                    obj.MaThietBi = ((row["MaThietBi"]).ToString());
            }
            catch { }
            
            try
            {
                if (row["SoLuong"] != DBNull.Value)
                    obj.SoLuong = float.Parse(row["SoLuong"].ToString());
            }
            catch { }          
                 
            try
            {
                if (row["Plant"] != DBNull.Value)
                    obj.Plant = (row["Plant"].ToString());
            }
            catch { }

            try
            {
                if (row["TenVTPT"] != DBNull.Value)
                    obj.TenThietBi = (row["TenVTPT"].ToString());
            }
            catch { }

            return obj;
        }

        private List<CHI_TIET_PDH_COMPONENT> GetCHI_TIET_PDH_COMPONENT(DataTable dt)
        {
            List<CHI_TIET_PDH_COMPONENT> lst = new List<CHI_TIET_PDH_COMPONENT>();
            CHI_TIET_PDH_COMPONENT obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);
                lst.Add(obj);
            }
            return lst;
        }

        public List<CHI_TIET_PDH_COMPONENT> SelectCHI_TIET_PDH_COMPONENT(int MaChiTietPDH)
        {
            SqlParameter[] par;
            string spName = "usp_SelectCHI_TIET_PDH_COMPONENTByMaChiTietPDH";
            DataTable dt = new DataTable();
            List<CHI_TIET_PDH_COMPONENT> lst = new List<CHI_TIET_PDH_COMPONENT>();
            try
            {
                SqlHelper.OpenConnection();

                par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaChiTietPDH", MaChiTietPDH);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PDH_COMPONENT(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public List<CHI_TIET_PDH_COMPONENT> SelectCHI_TIET_PYC_COMPONENT(int MaChiTietPYC)
        {
            SqlParameter[] par;
            string spName = "usp_SelectCHI_TIET_PYC_COMPONENTByMaChiTietPYC";
            DataTable dt = new DataTable();
            List<CHI_TIET_PDH_COMPONENT> lst = new List<CHI_TIET_PDH_COMPONENT>();
            try
            {
                SqlHelper.OpenConnection();

                par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaChiTietPYC", MaChiTietPYC);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PDH_COMPONENT(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        //public int DeleteCHI_TIET_PDH_COMPONENT(int MaChiTietPDH)
        //{

        //    string spName = "DELETE FROM CHI_TIET_PDH_COMPONENT WHERE MACHITIETPDH=" + MaChiTietPDH.ToString();
        //    int kq = -1;
        //    try
        //    {
        //        SqlHelper.OpenConnection();

        //        if (!SqlHelper.IsHaveTran())
        //            kq = SqlHelper.ExecuteNonQuery(spName, CommandType.Text);
        //        else
        //        {
        //            kq = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, CommandType.Text);
        //        }
              
        //    }
        //    catch (Exception ex)
        //    {
        //    }
        //    finally
        //    {
        //        if (!SqlHelper.IsHaveTran())
        //            SqlHelper.CloseConnection();
        //    }
        //    return kq;
        //}

        
    }
}
